package com.coda.service.impl;

import com.coda.constant.MessageConstant;
import com.coda.constant.RedisKey;
import com.coda.context.BaseContext;
import com.coda.exception.BaseException;
import com.coda.mapper.DictationResultsMapper;
import com.coda.mapper.DictationSessionMapper;
import com.coda.mapper.UserMapper;
import com.coda.pojo.DictationResult;
import com.coda.pojo.DictationSession;
import com.coda.pojo.User;
import com.coda.service.DictationSessionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DictationSessionServiceImpl implements DictationSessionService {

    private static final Logger log = LoggerFactory.getLogger(DictationSessionServiceImpl.class);
    @Autowired
    private DictationSessionMapper dictationSessionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DictationResultsMapper dictationResultsMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加默写会话
     *
     * @param
     * @return
     */
    @Override
    public int start(DictationSession dictationSession) {
        int result = dictationSessionMapper.insert(dictationSession);
        //如果redis中没有此次默写记录的id则添加
        if (redisTemplate.opsForValue().get(RedisKey.DICTATION_SESSION_ID) == null) {
            redisTemplate.opsForValue().set(RedisKey.DICTATION_SESSION_ID, dictationSession.getId());
        } else {
            //如果有默写记录的id删除，再添加
            redisTemplate.delete(RedisKey.DICTATION_SESSION_ID);
            redisTemplate.opsForValue().set(RedisKey.DICTATION_SESSION_ID, dictationSession.getId());
        }
        // todo 待测试 更新用户信息，当前默写时间
        User user = new User();
        user.setId(BaseContext.getCurrentId());
        user.setLastPractice(LocalDateTime.now());
        userMapper.update(user);
        return result;
    }

    /**
     * 结束默写
     *
     * @return
     */
    @Override
    public int endSession() {
        //获取redis中此次默写的id
        Integer sessionId = (Integer) redisTemplate.opsForValue().get(RedisKey.DICTATION_SESSION_ID);
        if (sessionId == null) {
            throw new BaseException(MessageConstant.SESSION_NOT_FIND);
        }
        //根据此次默写的记录的id查询所有默写的单词
        List<DictationResult> resultList = dictationResultsMapper.getDictationResultBySessionId(BaseContext.getCurrentId(), sessionId);

        //获取当前默写的次数
        int total = resultList.size();

        //答对的次数
        int isCorrectCount = 0;
        for (int i = 0; i < resultList.size(); i++) {
            if ("1".equals(resultList.get(i).getIsCorrect())) {
                isCorrectCount++;
            }
        }
        //更新当前记录
        DictationSession dictationSession = new DictationSession();
        dictationSession.setId((Integer) redisTemplate.opsForValue().get(RedisKey.DICTATION_SESSION_ID));
        dictationSession.setTotalWords(total);
        dictationSession.setCorrectCount(isCorrectCount);
        dictationSession.setEndTime(LocalDateTime.now());

        return dictationSessionMapper.update(dictationSession);
    }
}
